#!/bin/bash
# Run this to generate all the initial makefiles, etc.

DIE=0
CLEANONLY=0
NOCONFIGURE=0
UPDATEONLY=0
CONFIGURE_ARGS=()

for __BS_ARG__; do
	case "${__BS_ARG__}" in
		-h|--help)
			echo "Usage: ${0##*/} [OPTION]"
			echo
			echo 'Options:'	
			echo '    -h, --help          Show this help message'
			echo '    -z, --noconfigure   Prepare the build system without launching the'
			echo '                        configure process'
			echo '    -c, --clean         Remove all files generated by this script'
			echo
			echo 'If this script is invoked without the `--noconfigure` option all unrecognized'
			echo 'arguments will be passed to `configure`.'
			exit 0
			;;
		-z|--noconfigure)
			NOCONFIGURE=1
			;;
		-u|--update)
			# Private/unadvertised option for exporting version changes
			# from `configure.ac` (authors only)
			UPDATEONLY=1
			;;
		-c|--clean)
			CLEANONLY=1
			;;
		*)
			CONFIGURE_ARGS+=("${__BS_ARG__}")
			;;
	esac
done

srcdir="$(dirname ${0})"
test -z "${srcdir}" && srcdir=.

if test "${UPDATEONLY}" -ne 0; then
	"${0}" --clean && "${0}" --enable-extended-config && "${0}" --clean
	echo 'Package has been updated.'
	exit 0
fi

if test "${CLEANONLY}" -ne 0; then
	echo 'Cleaning package...'
	test -f 'Makefile' && make maintainer-clean
	rm -Rf autom4te.cache m4 build-aux no-dist `find . -type d -name .deps`
	rm -f aclocal.m4 compile config.* configure configure~ depcomp \
		install-sh libtool ltmain.sh missing `find . -name Makefile.in`
	echo 'Done'
	exit 0
fi

if test -n "${GNOME2_DIR}"; then
	ACLOCAL_FLAGS="-I ${GNOME2_DIR}/share/aclocal ${ACLOCAL_FLAGS}"
	LD_LIBRARY_PATH="${GNOME2_DIR}/lib:${LD_LIBRARY_PATH}"
	PATH="${GNOME2_DIR}/bin:${PATH}"
	export PATH
	export LD_LIBRARY_PATH
fi

test -f "${srcdir}/configure.ac" || {
	echo
	echo "**Error**: Directory \`${srcdir}\` does not look like the top-level package directory"
	echo
	exit 1
} >&2

autoconf --version < /dev/null > /dev/null 2>&1 || {
	echo
	echo '**Error**: You must have `autoconf` installed. Download the appropriate package'
	echo 'for your distribution, or get the source tarball at ftp://ftp.gnu.org/pub/gnu/'
	echo
	DIE=1
} >&2

if grep "^IT_PROG_INTLTOOL" "${srcdir}/configure.ac" >/dev/null; then
	intltoolize --version < /dev/null > /dev/null 2>&1 || {
		echo
		echo '**Error**: You must have `intltool` installed. You can get it from'
		echo 'https://freedesktop.org/wiki/Software/intltool/'
		echo
		DIE=1
	} >&2
fi

if grep "^AM_PROG_XML_I18N_TOOLS" "${srcdir}/configure.ac" >/dev/null; then
	xml-i18n-toolize --version < /dev/null > /dev/null 2>&1 || {
		echo
		echo '**Error**: You must have `xml-i18n-toolize` installed. You can get it from'
		echo 'https://download.gnome.org/sources/xml-i18n-tools/'
		echo
		DIE=1
	} >&2
fi

if grep "^LT_INIT" "${srcdir}/configure.ac" >/dev/null; then
	if [[ `test -z "${OSTYPE}" && uname -s || echo "${OSTYPE}"` =~ ^[Dd]arwin* ]]; then
		if test -z "${LIBTOOL}"; then
			echo 'macOS detected. Using glibtool.'
			LIBTOOL='glibtool'
		fi
		if test -z "${LIBTOOLIZE}"; then
			echo 'macOS detected. Using glibtoolize.'
			LIBTOOLIZE='glibtoolize'
		fi
	else
		if test -z "${LIBTOOL}"; then
			LIBTOOL='libtool'
		fi
		if test -z "${LIBTOOLIZE}"; then
			LIBTOOLIZE='libtoolize'
		fi
	fi
	("${LIBTOOL}" --version) < /dev/null > /dev/null 2>&1 || {
		echo
		echo '**Error**: You must have `libtool` installed. You can get it from'
		echo 'ftp://ftp.gnu.org/pub/gnu/'
		echo
		DIE=1
	} >&2
fi

if grep "^AM_GLIB_GNU_GETTEXT" "${srcdir}/configure.ac" >/dev/null; then
	grep "sed.*POTFILES" "${srcdir}/configure.ac" > /dev/null || \
	glib-gettextize --version < /dev/null > /dev/null 2>&1 || {
		echo
		echo '**Error**: You must have `glib` installed. You can get it from'
		echo 'https://gitlab.gnome.org/GNOME/glib'
		echo
		DIE=1
	} >&2
fi

(automake --version) < /dev/null > /dev/null 2>&1 || {
	echo
	echo '**Error**: You must have `automake` installed. You can get it from'
	echo 'ftp://ftp.gnu.org/pub/gnu/'
	echo
	DIE=1
	NO_AUTOMAKE=yes
} >&2

# if no automake, don't bother testing for aclocal
test -n "${NO_AUTOMAKE}" || (aclocal --version) < /dev/null > /dev/null 2>&1 || {
	echo
	echo '**Error**: Missing `aclocal`. The version of `automake` installed doesn'\''t appear'
	echo 'recent enough. You can get automake from ftp://ftp.gnu.org/pub/gnu/'
	echo
	DIE=1
} >&2

if test "${DIE}" -ne 0; then
	exit 1
fi

echo

if test "${NOCONFIGURE}" -eq 0; then
	echo 'I am going to prepare the build system and then run the `configure` script. If'
	echo 'you wish differently, please specify the `--noconfigure` argument on the'
	echo "\`${0##*/}\` command line."
	echo
	if test -z "$*"; then
		echo '**Warning**: I am going to run `configure` with no arguments. If you wish to'
		echo "pass any, please specify them on the \`${0##*/}\` command line."
		echo
	fi
else
	echo 'I am going to prepare the build system without running the `configure` script.'
	echo
	if test ${#CONFIGURE_ARGS[@]} -gt 0; then
		echo '**Warning**: The following arguments will be ignored:'
		for __IDX__ in ${!CONFIGURE_ARGS[@]}; do
			echo " $((__IDX__ + 1)). \`${CONFIGURE_ARGS[$__IDX__]}\`"
		done
		echo
	fi
fi

echo 'Preparing the build system... please wait'

case "${CC}" in
	xlc )
		am_opt=--include-deps
		;;
esac

for coin in `find "${srcdir}" -path "${srcdir}/CVS" -prune -o -name configure.ac -print`; do
	dr=`dirname "${coin}"`
	if test -f "${dr}/NO-AUTO-GEN"; then
		echo "skipping ${dr} -- flagged as no auto-gen"
	else
		echo "Processing '${dr}' ..."
		( cd "${dr}"

			aclocalinclude="${ACLOCAL_FLAGS}"
			[[ -d 'm4' ]] || mkdir 'm4'

			if grep "^AM_GLIB_GNU_GETTEXT" configure.ac >/dev/null; then
				echo "Creating ${dr}/aclocal.m4..."
				test -r "${dr}/aclocal.m4" || touch "${dr}/aclocal.m4"
				echo 'Running glib-gettextize...  Ignore non-fatal messages.'
				echo 'no' | glib-gettextize --force --copy
				echo "Making ${dr}/aclocal.m4 writable..."
				test -r "${dr}/aclocal.m4" && chmod u+w "${dr}/aclocal.m4"
			fi
			if grep "^IT_PROG_INTLTOOL" configure.ac >/dev/null; then
				echo 'Running intltoolize...'
				intltoolize --copy --force --automake
			fi
			if grep "^AM_PROG_XML_I18N_TOOLS" configure.ac >/dev/null; then
				echo 'Running xml-i18n-toolize...'
				xml-i18n-toolize --copy --force --automake
			fi
			if grep "^LT_INIT" configure.ac >/dev/null; then
				if test -z "${NO_LIBTOOLIZE}" ; then
					echo 'Running libtoolize...'
					"${LIBTOOLIZE}" --force --copy
				fi
			fi
			echo "Running aclocal ${aclocalinclude} ..."
			aclocal ${aclocalinclude}
			if grep "^A[CM]_CONFIG_HEADER" configure.ac >/dev/null; then
				echo 'Running autoheader...'
				autoheader
			fi
			echo "Running automake --gnu ${am_opt} ..."
			automake --add-missing --copy --gnu ${am_opt}
			echo 'Running autoconf...'
			autoconf
		)
	fi
done

if test "${NOCONFIGURE}" -eq 0; then
	echo -n "Running ${srcdir}/configure"
	for __CF_ARG__ in "${CONFIGURE_ARGS[@]}"; do echo -n " [${__CF_ARG__}]"; done
	echo ' ...'
	"${srcdir}/configure" "${CONFIGURE_ARGS[@]}" || exit "${?}"
	echo "Now type \`make\` to compile, or type \`${0} --clean\` to delete all the"
	echo 'files generated by this script.'
else
	echo 'Skipping the configure process. Type `configure --help` to list the configure'
	echo "options, or type \`${0} --clean\` to delete all the files generated by"
	echo 'this script.'
fi

# EOF

